﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Super_Wall_Breaker.Sources.TeamElements;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using Super_Wall_Breaker.Sources.GameElements;
using Microsoft.Xna.Framework.Audio;

namespace Super_Wall_Breaker.Sources.Screens
{
    /// <summary>
    /// The end screen comes up over the top of the game,
    /// when the time is up.
    /// </summary>
    class EndScreen : MenuScreen
    {
        TeamIndex? winnerIndex; // The position of the player who wins.

        /// <summary>
        /// Initializes a new instance of the <see cref="EndScreen"/> class.
        /// </summary>
        /// <param name="winner">The winning team.</param>
        public EndScreen(Team winner)
            : base("Time's Up !")
        {
            Sound sound = Sound.GetInstance; // sound singleton
            
            if (MediaPlayer.State == MediaState.Stopped)
            {                
                MediaPlayer.Volume = 0.3f;
                MediaPlayer.Play(sound.Audio_Never_Gonna_Give_You_Up);
            }

            sound.Applause.Play();

            if (winner == null)
                winnerIndex = null;
            else
                winnerIndex = winner.IdTeam;

            // Create our menu entries.            
            MenuEntry returnToMainMenu = new MenuEntry(((winnerIndex != null) ? "Team at the " + winnerIndex + " wins !" : "Draw!") + "\n You got Rick Roll'd !");

            // Hook up menu event handlers.            
            returnToMainMenu.Selected += ReturnToMainMenu;

            // Add entries to the menu.
            MenuEntries.Add(returnToMainMenu);
        }

        /// <summary>
        /// "Return to main menu" option event handler.
        /// </summary>
        /// <param name="sender"> The sender of the event.</param>
        /// <param name="e">The player index event arguments.</param>
        void ReturnToMainMenu(object sender, PlayerIndexEventArgs e)
        {
            const string message = "Enough Rick Roll ?";

            MessageBoxScreen confirmQuitMessageBox = new MessageBoxScreen(message);

            confirmQuitMessageBox.Accepted += ConfirmQuitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmQuitMessageBox, ControllingPlayer);
        }

        /// <summary>
        /// Quit event handler.
        /// </summary>
        /// <param name="sender">The sender of the quit event.</param>
        /// <param name="e">"The args event."</param>
        void ConfirmQuitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
        {
            LoadingScreen.Load(ScreenManager, false, null, new BackgroundScreen(),
                                                           new MainMenuScreen());
        }        
    }
}
